package com.lianniu.mgr.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class FileUtil {
	
	// cellFlog 有效数据表示字段，这个字段没有说明已经数据
	public static List<Map<String, String>> getXlsInfo(InputStream instream, int cellFlog) throws Exception {  
		//构建Workbook对象, 只读Workbook对象   
		  
//        InputStream instream = new FileInputStream("C:/Users/Administrator/Desktop/项目管理人员信息.xls");   

        Workbook readwb = Workbook.getWorkbook(instream);   

        //Sheet的下标是从0开始   

        //获取第一张Sheet表   

        Sheet readsheet = readwb.getSheet(0);   

        //获取Sheet表中所包含的总列数   

        int rsColumns = readsheet.getColumns();   

        //获取Sheet表中所包含的总行数   

        int rsRows = readsheet.getRows();   

        //获取指定单元格的对象引用   
        Map<String, String> map;
        
        List<String> listTitle = new ArrayList<>();
        List<Map<String, String>> list = new ArrayList<>();

        Cell cell;
        for (int i = 0; i < rsRows; i++)   

        {   
        	cell = readsheet.getCell(cellFlog, i);
        	if(cell.getContents() == null || "".equals(cell.getContents())){
        		break;
        	}
        	if(i == 0){
	        	for (int j = 0; j < rsColumns; j++)   
	            {   
	            	cell = readsheet.getCell(j, i);   
                	listTitle.add(cell.getContents());
//                	System.out.print(cell.getContents() + " ");  
	            }  
        	}else{
        		map = new HashMap<>();
                for (int j = 0; j < rsColumns; j++)   
                {   
                	cell = readsheet.getCell(j, i);   
            		map.put(listTitle.get(j), cell.getContents());
                }  
                list.add(map);
        	}
        	
        }   

        return list;
	}  
	
	
	// cellFlog 有效数据表示字段，这个字段没有说明已经数据
		public static List<Map<String, String>> getXlsInfo(String url, int cellFlog) throws Exception {  
			//构建Workbook对象, 只读Workbook对象   
			  
	        //直接从本地文件创建Workbook   
			InputStream instream = new FileInputStream(url);   
//	        InputStream instream = new FileInputStream("C:/Users/Administrator/Desktop/项目管理人员信息.xls");   

	        Workbook readwb = Workbook.getWorkbook(instream);   

	        //Sheet的下标是从0开始   

	        //获取第一张Sheet表   

	        Sheet readsheet = readwb.getSheet(0);   

	        //获取Sheet表中所包含的总列数   

	        int rsColumns = readsheet.getColumns();   

	        //获取Sheet表中所包含的总行数   

	        int rsRows = readsheet.getRows();   

	        //获取指定单元格的对象引用   
	        Map<String, String> map;
	        
	        List<String> listTitle = new ArrayList<>();
	        List<Map<String, String>> list = new ArrayList<>();

	        Cell cell;
	        for (int i = 0; i < rsRows; i++)   

	        {   
	        	cell = readsheet.getCell(cellFlog, i);
	        	if(cell.getContents() == null || "".equals(cell.getContents())){
	        		break;
	        	}
	        	if(i == 0){
		        	for (int j = 0; j < rsColumns; j++)   
		            {   
		            	cell = readsheet.getCell(j, i);   
	                	listTitle.add(cell.getContents());
//	                	System.out.print(cell.getContents() + " ");  
		            }  
	        	}else{
	        		map = new HashMap<>();
	                for (int j = 0; j < rsColumns; j++)   
	                {   
	                	cell = readsheet.getCell(j, i);   
	            		map.put(listTitle.get(j), cell.getContents());
//	                    System.out.print(cell.getContents() + " ");  
	                }  
	                list.add(map);
	        	}
	        	
//	            System.out.println();   
	        }   

//	        //利用已经创建的Excel工作薄,创建新的可写入的Excel工作薄   
	//
//	        jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(new File(   
	//
//	                "F:/红楼人物1.xls"), readwb);   
	//
//	        //读取第一张工作表   
	//
//	        jxl.write.WritableSheet ws = wwb.getSheet(0);   
	//
//	        //获得第一个单元格对象   
	//
//	        jxl.write.WritableCell wc = ws.getWritableCell(0, 0);   
	//
//	        //判断单元格的类型, 做出相应的转化   
	//
//	        if (wc.getType() == CellType.LABEL)    
	//
//	        {   
	//
//	            Label l = (Label) wc;   
	//
//	            l.setString("新姓名");   
	//
//	        }   

	        //写入Excel对象   

//	        wwb.write();   
	//
//	        wwb.close();   
	        return list;
		}  
	public void getUrl() throws Exception {  
//        response.setContentType("text/html");     
//        response.setCharacterEncoding("UTF-8");  
//        String realDir = request.getSession().getServletContext().getRealPath("");  
//        String contextpath = request.getContextPath();  
//        String basePath = request.getScheme() + "://"  
//        + request.getServerName() + ":" + request.getServerPort()  
//        + contextpath + "/";  
//  
//        try {  
//        String filePath = "uploadfiles";  
//        String realPath = realDir+"\\"+filePath;  
//        //判断路径是否存在，不存在则创建  
//        File dir = new File(realPath);  
//        if(!dir.isDirectory())  
//            dir.mkdir();  
//  
//        if(ServletFileUpload.isMultipartContent(request)){  
//  
//            DiskFileItemFactory dff = new DiskFileItemFactory();  
//            dff.setRepository(dir);  
//            dff.setSizeThreshold(1024000);  
//            ServletFileUpload sfu = new ServletFileUpload(dff);  
//            FileItemIterator fii = null;  
//            fii = sfu.getItemIterator(request);  
//            String title = "";   //图片标题  
//            String url = "";    //图片地址  
//            String fileName = "";  
//            String state="SUCCESS";  
//            String realFileName="";  
//            while(fii.hasNext()){  
//                FileItemStream fis = fii.next();  
//  
//                try{  
//                    if(!fis.isFormField() && fis.getName().length()>0){  
//                        fileName = fis.getName();  
//                        Pattern reg=Pattern.compile("[.]jpg|png|jpeg|gif$");  
//                        Matcher matcher=reg.matcher(fileName);  
//                        if(!matcher.find()) {  
//                            state = "文件类型不允许！";  
//                            break;  
//                        }  
//                        realFileName = new Date().getTime()+fileName.substring(fileName.lastIndexOf("."),fileName.length());  
//                        url = realPath+"\\"+realFileName;  
//                         //url = "D:\\workspace";
//  
//                        BufferedInputStream in = new BufferedInputStream(fis.openStream());//获得文件输入流  
//                        FileOutputStream a = new FileOutputStream(new File(url));  
//                        BufferedOutputStream output = new BufferedOutputStream(a);  
//                        Streams.copy(in, output, true);//开始把文件写到你指定的上传文件夹  
//                    }else{  
//                        String fname = fis.getFieldName();  
//  
//                        if(fname.indexOf("pictitle")!=-1){  
//                            BufferedInputStream in = new BufferedInputStream(fis.openStream());  
//                            byte c [] = new byte[10];  
//                            int n = 0;  
//                            while((n=in.read(c))!=-1){  
//                                title = new String(c,0,n);  
//                                break;  
//                            }  
//                        }  
//                    }  
//  
//                }catch(Exception e){  
//                    e.printStackTrace();  
//                }  
//            }  
//            response.setStatus(200);  
//            String retxt ="{src:'"+basePath+filePath+"/"+realFileName+"',status:success}";
//            System.out.println(retxt);
//            response.getWriter().print(retxt);  
//        }  
//        }catch(Exception ee) {  
//            ee.printStackTrace();  
//        }  
          
    }  
	
	public static void main(String[] args) throws Exception {
		List<Map<String,String>> list = getXlsInfo("", 2);
		System.out.print(" ");   
	}



}
